import { ref, unref, computed, MaybeRef } from "vue"
import { ClosuredDataSource, PaginationParams, SearchDataSource } from "@/types"

export function useMockSearchDataSource(): SearchDataSource {
  return function (
    params: MaybeRef<{ keyword: string }>,
    pagination: MaybeRef<PaginationParams>,
  ) {
    const isFetching = ref(false)
    return {
      isFetching,
      data: computed(() => {
        const content = []
        for (let i = 0; i < unref(pagination).size; i++) {
          content.push({
            label: `选项${unref(params).keyword}` + i,
            value: `prop${unref(params).keyword}` + i,
          })
        }
        return {
          content,
          page: unref(pagination).page,
          size: unref(pagination).size,
          total: 100,
        }
      }),
      error: ref(null),
      execute() {
        isFetching.value = true
        return new Promise((resolve) => {
          setTimeout(() => {
            isFetching.value = false
            resolve(null)
          }, 1000)
        })
      },
    }
  }
}

export function useMockClosuredDataSource(): ClosuredDataSource {
  return (pagination: MaybeRef<PaginationParams>) => {
    return {
      isFetching: ref(false),
      data: computed(() => {
        const content = []
        for (let i = 0; i < unref(pagination).size; i++) {
          content.push({
            label: `选项` + i,
            value: `prop` + i,
          })
        }
        return {
          content,
          page: unref(pagination).page,
          size: unref(pagination).size,
          total: 100,
        }
      }),
      error: ref(null),
      execute() {
        return Promise.resolve(null)
      },
    }
  }
}
